package com.example.calccalories;

//import com.aplimovil.todo.ToDoDBAdapter.ToDoDBOpenHelper;


import android.R.bool;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbAdapter {
	private static final String DATABASE_NAME = "calories.db";
	private static final String TABLE_CUENTAS = "cuentas";
	private static final int DATABASE_VERSION = 1;

	public static final String KEY_ID = "_id";
	public static final String NOMBRES = "nombres";
	public static final String ESTATURA = "estarura";
	public static final String PESO = "peso";
	public static final String CORREO = "correo";
	public static final String SEXO = "sexo";
	public static final String CONTRASENIA = "contrasenia";
	public static final String NACIMIENTO = "nacimiento";
	
	private final Context ncontext;
	private SQLiteDatabase ndb;
	private BdHelper bdhelper;
	
	public DbAdapter(Context _context) {
		this.ncontext = _context;
		bdhelper = new BdHelper(ncontext);
	}
	
	public static class BdHelper extends SQLiteOpenHelper
	{
		public static final String CreateTableCuentas= "create table "+ TABLE_CUENTAS + " (" 
				+ KEY_ID + " integer primary key autoincrement, " 
				+ NOMBRES + " text not null, " 
				+ ESTATURA + " intrger not null, " 
				+ PESO + " integer not null, "
				+ CORREO + " text not null, "
				+ SEXO + " text not null, "
				+ CONTRASENIA + " integer not null, "
				+ NACIMIENTO + " text not null);";

		public BdHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(CreateTableCuentas);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			
			Log.w("TaskDBAdapter", "Upgrading from version " + oldVersion
					+ " to " + newVersion
					+ ", which will destroy all old data");
			
			db.execSQL("drop table if exist "+ TABLE_CUENTAS);
			onCreate(db);
		}
	}

	public void close() {
		ndb.close();
	}

	public void open() throws SQLiteException {
		try {
			ndb = bdhelper.getWritableDatabase();
		} catch (SQLiteException ex) {
			ndb = bdhelper.getReadableDatabase();
		}
	}
	
	public long CreateCuenta(String nombres, int estarura, int peso, String correo, String sexo,  String contrasenia, String nacimiento)
	{
		ContentValues cv = new ContentValues();
		cv.put(NOMBRES, nombres);
		cv.put(ESTATURA, estarura);
		cv.put(PESO, peso);
		cv.put(CORREO, correo);
		cv.put(SEXO, sexo);
		cv.put(CONTRASENIA, contrasenia);
		cv.put(NACIMIENTO, nacimiento);
		return ndb.insert(TABLE_CUENTAS,null,cv);
	}
	
	public boolean IsUser( String user,  int pass) {
		Cursor cursor = ndb.query(TABLE_CUENTAS,new String[] {CORREO,CONTRASENIA},CORREO + "=" + user + " and "+CONTRASENIA+"="+pass,null,null,null,null,null);
		System.out.println("ultima prueba-1");
		boolean bandera;
		if (cursor.getCount()==0) bandera = false;
		else bandera = true;
		return bandera;
	}
	
}
